// import 'package:flutter_app/entity/product_entity.dart';
// import 'package:flutter_app/helper.dart';
// import 'package:flutter_app/services/api.dart';
import 'package:get/get.dart';
import 'package:sp_util/sp_util.dart';
import 'package:yp/entity/product_entity.dart';
import 'package:yp/helper.dart';
import 'package:yp/services/api.dart';

import 'state.dart';

class MaterialCreateLogic extends GetxController {
  final state = MaterialCreateState();

  @override
  void onInit() {
    super.onInit();
    ProductEntity item = Get.arguments == null ? null : Get.arguments['item'];

    if (item != null) {
      // var params = {
      //   "config_name": "platform_ec_shop_num_id",
      // };

      double expectPrice = 0.00;

      if (item.expectPrice != null) {
        expectPrice = item.expectPrice.toDouble();
      }

      state.selectShopData = {
        "image_default_id": item.imageSrc,
        "price": item.price,
        "title": item.itemName,
        "expect_price": expectPrice,
      };

      state.goodsList = [];
      var params22 = {
        "item_num_id": item.itemNumId,
        "style_num_id": item.styleNumId,
        "shop_id": item.ecShopNumId,
      };
      state.goodsList.add(params22);

      state.controller.text = item.itemName;
      // Api.MaterialGoodsSelect1(params).listen((event) {
      //   state.configValue = event["config_value"].toString();
      //   var params1 = {
      //     "page": 1,
      //     "page_size": 10,
      //     "approve_status": "onsale",
      //     "args": {
      //       "shop_id": state.configValue,
      //       "approve_status": "onsale",
      //     }
      //   };
      //   Api.MaterialGoodsSelect2(params1).listen((event) {
      //     List arr = event["data"]["rows"];
      //     state.shopList = arr;
      //     update();
      //   });
      // });
    }

    var params = {
      'usr_num_id': SpUtil.getInt('empeNumId').toString(),
      "page_no": 1,
      "cloud_ec_shop_num_id": 1,
      "page_size": 10,
      'user_type': 1,
      'use_platform': 2,
    };
    print("##############$params");
    //获取选择商品列表数据
    Api.selectGoods(params).listen((event) {
      state.shopList = event["lists"];
      update();
    });

    update();
  }

  /// 下拉刷新
  void refreshData() {
    state.shopList = [];
    state.page = 1;
    var params = {
      'usr_num_id': SpUtil.getInt('empeNumId').toString(),
      "page_no": 1,
      "cloud_ec_shop_num_id": 1,
      "page_size": 10,
      'user_type': 1,
      'use_platform': 2,
    };
    if (state.controller1.text.isNotEmpty) {
      params.addAll({"title": state.controller1.text});
    }
    print("#####$params");
    Api.selectGoods(params).listen((event) {
      List arr = event["lists"];
      state.shopList = arr;
      state.controller2.refreshCompleted(resetFooterState: true);
      update();
    });
  }

  /// 上拉加载更多
  void loadMoreData() {
    state.page = state.page + 1;

    var params = {
      'usr_num_id': SpUtil.getInt('empeNumId').toString(),
      "page_no": state.page,
      "cloud_ec_shop_num_id": 1,
      "page_size": 10,
      'user_type': 1,
      'use_platform': 2,
    };
    if (state.controller1.text.isNotEmpty) {
      params.addAll({"title": state.controller1.text});
    }
    Api.selectGoods(params).listen((event) {
      List arr = event["lists"];
      if (arr.length > 0) {
        state.shopList.addAll(arr);
        state.controller2.loadComplete();
      } else {
        state.controller2.loadNoData();
      }
      update();
    });
  }

  @override
  void onReady() {
    super.onReady();
  }

  void onShopSelected(int index) {
    state.goodsList = [];
    var params = {
      "item_num_id": state.shopList[index]["default_item_num_id"],
      "style_num_id": state.shopList[index]["style_num_id"],
      "shop_id": state.shopList[index]["shop_num_id"],
    };
    state.currentShopIndex = index;
    state.goodsList.add(params);
    update();
  }

  void onAdrSelected(int index) {
    state.currentAdreIndex = index;
    update();
  }

  //素材发布接口文档
  void kocMaterialCreate() {
    if (state.goodsList.length == 0) {
      toast("请选择商品");
      return;
    }
    if (state.controller.text.isEmpty) {
      toast("请输入推荐语");
      return;
    }
    var params = {
      "personnel_num_id": SpUtil.getInt('empeNumId'),
      //人员类型  1:网格员 2:KOC
      "personnel_type": 2,
      "urls": state.fileList,
      "dynamic_context": state.controller.text,
      "related_item_num_ids": state.goodsList,
      "location":
          state.selectAdrData != null ? state.selectAdrData["name"] : '',
    };
    print('>>>>>>>>>>>>>>>>>>${params}');
    // return;kan
    Api.kocMaterialCreate(params).listen((event) {
      if (event["code"] == 0) {
        toast("提交成功");
        Get.back(result: true);
      }
    });
  }

  void search(value) {
    state.page = 1;
    refreshData();
  }
}
